package com.sd.activity;
import java.util.Iterator;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.alex.log.ALog;
import com.sd.activity.r.R;
import com.sd.util.HtmlFinish;
import com.sd.util.WebUtil;
import com.umpay.huafubao.plugin.android.intf.Huafubao;

/**
 * 
 * 
 * @author Alex.Lu
 * 
 */
public class CHLMessageRechargeActivity extends BaseActivity implements
		OnClickListener, HtmlFinish , OnCheckedChangeListener{

	
	private LinearLayout mMainLL;
	private ScrollView scroll;
	//private Huafubao huafubao = null;
	private ImageButton mReturn;
	private static int mMsg;
	private EditText mPhoneNoET;
	private EditText mAmountET;
	private Button mRechargeBT;
	private TextView mTitleTV;
	private String merId = "3284";
	private String goodsId = "050";
	private String amount ="500";
	private RadioGroup mRadioGroup;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_recharge_message_chl);
		
		/*if(!AppInit.getInstance().getSIM().getNativePhoneNumber().equals("")
			&& AppInit.getInstance().getSIM().getProvidersName().equals("中国移动")){
			LinearLayout accountLL = (LinearLayout) findViewById(R.id.accountLL);
			//accountLL.addView(ViewUtil.getAccountView(this, "account", "value"));

			LinearLayout mWarmTipLL = (LinearLayout)findViewById(R.id.warmtipLL);
			mWarmTipLL.addView(ViewUtil.getWarmTips(this, R.string.warmtip5));
			
			mTitleTV = (TextView) findViewById(R.id.title);

			mPhoneNoET = (EditText) findViewById(R.id.phone_no);
			//mPhoneNoET.setText(AppInit.getInstance().getSIM().getNativePhoneNumber());
			mAmountET = (EditText) findViewById(R.id.amount);

			mRechargeBT = (Button) findViewById(R.id.recharge);
			mRechargeBT.setOnClickListener(this);

			mRadioGroup = (RadioGroup)findViewById(R.id.rechargacmount);
			mRadioGroup.setOnCheckedChangeListener(this);
			mReturn = (ImageButton) findViewById(R.id.return_btn);
			mReturn.setOnClickListener(this);
		}else{*/
		
			mMainLL = (LinearLayout)findViewById(R.id.recharge_main);
			mMainLL.addView(WebUtil.getWevView(this, "http://wap.qidian.cn/wap2/user/checkMobile.do?mtd=checkMobile&sid="+BookStoreActivity.user));
			mMainLL.setVisibility(View.VISIBLE);
			scroll = (ScrollView)findViewById(R.id.scrollView1);
			scroll.setVisibility(View.GONE);
			mReturn = (ImageButton) findViewById(R.id.return_btn);
			mReturn.setOnClickListener(this);
		//}
	}
	
	

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int v_id = v.getId();

		switch (v_id) {
		case R.id.return_btn:
			finish();
			break;

		case R.id.recharge:
			/*huafubao = new Huafubao(this, this);
			String phonenum = mPhoneNoET.getText().toString();
			mAmountET.getText().toString();
			
			String postUrl = "http://wap.qidian.cn/wap2/pay/unihjsmschannellist.do?mtd=unihjsmschannellist&sid=" + BookStoreActivity.user;
			StringBuffer sbuf = new StringBuffer();
			sbuf.append("mobile=");
			sbuf.append(phonenum);
			sbuf.append("&c=unicom");
			sbuf.append("&pri=2");
			ALog.e("postUrl:"+postUrl);
			WebUtil.getHtml(this,this,postUrl,sbuf.toString());*/
		/*	
			Map<String, String> map = new HashMap<String, String>();
			map.put(Huafubao.MERID_STRING, merId);
			map.put(Huafubao.GOODSID_STRING, goodsId);
			String orderId = String.valueOf(System.currentTimeMillis());
			if(orderId.length() >= 13){
				orderId = orderId.substring(0,12);
			}
			map.put(Huafubao.ORDERID_STRING, orderId);
			Date today=new Date();
			SimpleDateFormat f=new SimpleDateFormat("yyyyMMdd");
			String time=f.format(today);
			map.put(Huafubao.MERDATE_STRING,time);
			map.put(Huafubao.AMOUNT_STRING, amount);
			String str = BookStoreActivity.user.substring(0, BookStoreActivity.user.indexOf(";;"));
			map.put(Huafubao.MERPRIV_STRING, str);
			map.put(Huafubao.EXPAND_STRING, str);
			Log.e("", " 订单号:"+orderId+"  商户号:"+merId+"  商品号:"+goodsId
					+"  订单号:"+orderId +"  商户日期:" +time +" 商品金额:"+ 
					amount +"商户私有信息:"+str);
			*//**
			 * 下订单
			 * 参数说明：
			 * map：商户需要提交给支付平台的数据
			 * isNetResult：boolean，该说明用户支付结果的获取方式。
			 * 为true时，支付结果要联网去支付平台服务器查询支付结果，
			 * 为false时，支付结果不联网去服务器获得，只要用户确认支付过以后就返回支付成功。
			 *//*
			huafubao.setRequest(map, true);*/
			break;
		default:
			break;
		}
	}
	
	
	private void sendSMS(String phonenum, String content) {
		if ((phonenum == null || "".equals(phonenum))
				|| (content == null || "".equals(content))) {
		} else {
			SmsManager manage = SmsManager.getDefault(); // 取得默认的SmsManager用于短信的发送
			List<String> all = manage.divideMessage(content); // 短信的内容是有限的，要根据短信长度截取。逐条发送
			Iterator<String> it = all.iterator();
			while (it.hasNext()) {
				manage.sendTextMessage(phonenum, null, it.next(), null, null); // 逐条发送短息
			}
		}

	}


	@Override
	public void onFinish(String html) {
		// TODO Auto-generated method stub
		ALog.e("onfinish:"+html);
	}



    /*
    *//**
     * 处理下定单过程中出现的异常
     * code：错误代码，由Huafubao类定义
     * msg：错误文字描述信息，由Huafobao类定义
     *//*
	@Override
	public boolean onError(int code, String msg) {
		boolean flag = false;
		switch (code) {
		case Huafubao.ERROR_NO_INSTALL:  // 没有安装话付宝支付服务
			flag = true;  
			break;
		case Huafubao.ERROR_NO_MERID:   // 商户号为空
			flag = false;
			break;
		case Huafubao.ERROR_NO_GOODSID:  // 商品号(计费点)为空
			flag = false;
			break;
		case Huafubao.ERROR_NO_MERDATE:  // 日期为空
			flag = false;
			break;
		case Huafubao.ERROR_NO_AMOUNT:  // 商品金额为空
			flag = true;  // 返回值为true表示此错误由话付宝处理，为false表示由商户自己写代码处理。
			                    // 话付宝处理此类问题的方法是：使用Toast显示错误信息，并退出支付流程。
			break;
		case Huafubao.ERROR_NO_NETWORK: // 手机没有联网
			flag =true;
			break;
		default:
			break;
		}
		if (!flag) {
			// 处理返回值为false的错误。
			// 此处的处理方式是对话框提示出来。
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle("商户提示").setMessage(msg).setPositiveButton("确定", null);
			builder.create().show();
		}
		return flag;
	}	*/
	
	/**
     * 支付结果的通知
     * 商户要判断requestCode和resultCode
     * 最后在data对象中取出支付结果
     * 支付结果用boolean变量表示
     * true：支付成功， false：支付失败
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	if (requestCode == Huafubao.HUAFUBAOREQCODE) {
        	if (data == null) {
        		Toast.makeText(this, "data == null, 支付失败, requestCode = " + requestCode + ", resultCode = " + resultCode, Toast.LENGTH_LONG).show();
        	} else {
            	boolean succ = data.getExtras().getBoolean(Huafubao.SUCC);  // 取出表示用户支付结果的变量
            	if (succ) {
            		Toast.makeText(this, "支付成功, requestCode = " + requestCode + ", resultCode = " + resultCode, Toast.LENGTH_LONG).show();
            	} else {
            		Toast.makeText(this, "支付失败, requestCode = " + requestCode + ", resultCode = " + resultCode, Toast.LENGTH_LONG).show();
            	}	
        	}	
    	}
    	super.onActivityResult(requestCode, resultCode, data);
    }



	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		// TODO Auto-generated method stub
		if(checkedId == R.id.radio0){
			goodsId = "050";
			amount = "500";
		}else if(checkedId == R.id.radio1){
			goodsId = "100";
			amount = "1000";
		}else if(checkedId == R.id.radio2){
			goodsId = "300";
			amount = "3000";
		}
		
	}

}